using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Net;
using System.Drawing;

namespace PT.Common
{
    /// <summary>
    /// 网络GET POST类
    /// </summary>
    public class YSNetClass
    {
       
        #region 取获某一页面
        /// <summary>
        /// 取获某一页面
        /// </summary>
        /// <param name="url">设置网址</param>
        /// <param name="referer">上一页地址</param>
        /// <param name="cookieReq">cookie对象</param>
        /// <param name="proxy">设置http代理</param>
        /// <param name="redirect">是否接受重定向</param>
        /// <returns></returns>
        public string Get(string url, string referer, CookieContainer cookieReq, WebProxy proxy, bool redirect)
        {
            string text = "";
            try
            {
                string text2 = "";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                if (proxy != null)
                {
                    request.Proxy = proxy;
                }
                request.Referer = referer;
                request.Method = "GET";
                request.KeepAlive = true;
                request.CookieContainer = cookieReq;
                request.AllowAutoRedirect = redirect;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                response.Cookies = cookieReq.GetCookies(request.RequestUri);
              
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, Encoding.Default);
                text2 = reader.ReadToEnd();

                reader.Close();
                responseStream.Close();
                response.Close();
                text = text2;
                reader = null;
                text2 = null;
                responseStream = null;
                request = null;
                response = null;
            }
            catch
            {
                text = "";
            }
          
            return text;
        }
        #endregion

        #region 取获某一页面(可设置接收数据的编码)
        /// <summary>
        /// 取获某一页面
        /// </summary>
        /// <param name="url">设置网址</param>
        /// <param name="referer">上一页地址</param>
        /// <param name="cookieReq">cookie对象</param>
        /// <param name="proxy">设置http代理</param>
        /// <param name="redirect">是否接受重定向</param>
        /// <param name="encoding">设置编码 如"gb2312" "UTF-8"</param>
        /// <returns></returns>
        public string Get(string url, string referer, CookieContainer cookieReq, WebProxy proxy, bool redirect,string encoding)
        {
            string text = "";
            try
            {
                string text2 = "";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                if (proxy != null)
                {
                    request.Proxy = proxy;
                }
                request.Referer = referer;
                request.Method = "GET";
                request.KeepAlive = true;
                request.CookieContainer = cookieReq;
                request.AllowAutoRedirect = redirect;
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                response.Cookies = cookieReq.GetCookies(request.RequestUri);

                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
                text2 = reader.ReadToEnd();

                reader.Close();
                responseStream.Close();
                response.Close();
                text = text2;
                reader = null;
                text2 = null;
                responseStream = null;
                request = null;
                response = null;
            }
            catch
            {
                text = "";
            }

            return text;
        }
        #endregion 

        #region 访问某一页面,不要求返回
        /// <summary>
        /// 访问某一页面,不要求返回
        /// </summary>
        /// <param name="url">设置网址</param>
        /// <param name="referer">上一页地址</param>
        /// <param name="cookieReq">cookie对象</param>
        /// <param name="proxy">设置http代理</param>
		public void GetVoid(string url, string referer, CookieContainer cookieReq, WebProxy proxy)
		{
			string text = "";
			try
			{
				string text2 = "";
				HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
				if (proxy != null)
				{
					request.Proxy = proxy;
				}
				request.Referer = referer;
				request.Method = "GET";
				request.KeepAlive = true;
				request.CookieContainer = cookieReq;
				request.AllowAutoRedirect = true;
				request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
				HttpWebResponse response = (HttpWebResponse)request.GetResponse();
				response.Cookies = cookieReq.GetCookies(request.RequestUri);
              
				Stream responseStream = response.GetResponseStream();
				StreamReader reader = new StreamReader(responseStream, Encoding.Default);
				text2 = reader.ReadToEnd();

				reader.Close();
				responseStream.Close();
				response.Close();
				text = text2;
				reader = null;
				text2 = null;
				responseStream = null;
				request = null;
				response = null;
			}
			catch
			{
			}

        }
        #endregion 

        #region 发送数据
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="url">发送地址</param>
        /// <param name="referer">上一页地址</param>
        /// <param name="cookieReq">cookie对象</param>
        /// <param name="postData">发送数据</param>
        /// <param name="proxy">设置http代理</param>
        /// <param name="redirect">是否跟随重定向响应</param>
        /// <returns></returns>
        public string Post(string url, string referer, CookieContainer cookieReq, string postData, WebProxy proxy, bool redirect)
        {
            string html = "";
            try
            {
                string text2 = "";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                if (proxy != null)
                {
                    request.Proxy = proxy;
                }
                request.Referer = referer;  //设置http标头置
                request.KeepAlive = true;   // 是否与 internet 建立持久连接
                request.AllowAutoRedirect = redirect;  //是否跟随重定向响应
                request.UserAgent ="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";  //设置http用户标头值
                request.CookieContainer = cookieReq;    //设置关联的 CookieContainer
                request.ContentType = "application/x-www-form-urlencoded";   //设置http用户标头值
                request.Method = "POST";   // 设置请求方法
                byte[] bytes = Encoding.Default.GetBytes(postData);  //编码数据
                request.ContentLength = bytes.Length;    //设置http用户标头值  字节长度
                request.GetRequestStream().Write(bytes, 0, bytes.Length);   // 写入请求数据
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();  //实现 响应
                response.Cookies = cookieReq.GetCookies(response.ResponseUri);      // 获取 设置cookies
               
                Stream responseStream = response.GetResponseStream();        //获取流
                StreamReader reader = new StreamReader(responseStream, Encoding.Default);  //编码
                text2 = reader.ReadToEnd();  //从流当前位置读到末尾 
                reader.Close();
                responseStream.Close();
                response.Close();
                html = text2;
                reader = null;
                text2 = null;
                responseStream = null;
                request = null;
                response = null;
                bytes = null;
            }
            catch
            {
                html = "";
            }
           
            return html;
        }
        #endregion 

        #region 发送数据(可设置发送数据和接收数据的编码)
        /// <summary>
        /// 发送数据(可设置发送数据和接收数据的编码)
        /// </summary>
        /// <param name="url">发送地址</param>
        /// <param name="referer">上一页地址</param>
        /// <param name="cookieReq">cookie对象</param>
        /// <param name="postData">发送数据</param>
        /// <param name="proxy">设置http代理</param>
        /// <param name="redirect">是否跟随重定向响应</param>
        /// <param name="PostDataEncoding">设置发送数据的编码 如"gb2312" "UTF-8"</param>
        /// <param name="encoding">设置接收数据的编码 如"gb2312" "UTF-8"</param>
        /// <returns></returns>
        public string Post(string url, string referer, CookieContainer cookieReq, string postData, WebProxy proxy, bool redirect, string PostDataEncoding, string encoding)
        {
            string html = "";
            try
            {
                string text2 = "";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                if (proxy != null)
                {
                    request.Proxy = proxy;
                }
                request.Referer = referer;  //设置http标头置
                request.KeepAlive = true;   // 是否与 internet 建立持久连接
                request.AllowAutoRedirect = redirect;  //是否跟随重定向响应
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";  //设置http用户标头值
                request.CookieContainer = cookieReq;    //设置关联的 CookieContainer
                request.ContentType = "application/x-www-form-urlencoded";   //设置http用户标头值
                request.Method = "POST";   // 设置请求方法
                byte[] bytes = Encoding.GetEncoding(PostDataEncoding).GetBytes(postData);  //编码数据
                request.ContentLength = bytes.Length;    //设置http用户标头值  字节长度
                request.GetRequestStream().Write(bytes, 0, bytes.Length);   // 写入请求数据
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();  //实现 响应
                response.Cookies = cookieReq.GetCookies(response.ResponseUri);      // 获取 设置cookies

                Stream responseStream = response.GetResponseStream();        //获取流
                StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));  //编码
                text2 = reader.ReadToEnd();  //从流当前位置读到末尾 
                reader.Close();
                responseStream.Close();
                response.Close();
                html = text2;
                reader = null;
                text2 = null;
                responseStream = null;
                request = null;
                response = null;
                bytes = null;
            }
            catch
            {
                html = "";
            }

            return html;
        }
        #endregion 

        #region 发送数据(不要求返回)

        /// <summary>
        /// 发送数据(不要求返回)
        /// </summary>
        /// <param name="url">发送地址</param>
        /// <param name="referer">上一页地址</param>
        /// <param name="cookieReq">cookie对象</param>
        /// <param name="postData">发送数据</param>
        /// <param name="proxy">设置http代理</param>
        /// <param name="redirect">是否跟随重定向响应</param>
		public void Post_void(string url, string referer, CookieContainer cookieReq, string postData, WebProxy proxy, bool redirect)
		{
			string html = "";
			try
			{
				string text2 = "";
				HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
				if (proxy != null)
				{
					request.Proxy = proxy;
				}
				request.Referer = referer;
				request.KeepAlive = true;
				request.AllowAutoRedirect = redirect;
				request.UserAgent ="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
				request.CookieContainer = cookieReq;
				request.ContentType = "application/x-www-form-urlencoded";
				request.Method = "POST";
                byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(postData);
				request.ContentLength = bytes.Length;
				request.GetRequestStream().Write(bytes, 0, bytes.Length);
				HttpWebResponse response = (HttpWebResponse)request.GetResponse();
				response.Cookies = cookieReq.GetCookies(response.ResponseUri);
               
				Stream responseStream = response.GetResponseStream();
				StreamReader reader = new StreamReader(responseStream, Encoding.Default);
				text2 = reader.ReadToEnd();
				reader.Close();
				responseStream.Close();
				response.Close();
				html = text2;
				reader = null;
				text2 = null;
				responseStream = null;
				request = null;
				response = null;
				bytes = null;
			}
			catch
			{
			}
        }
        #endregion 

        #region 发送数据(另外的无名方法)

        /// <summary>
        /// 发送数据(另外的无名方法)
        /// </summary>
		public string post3(string url,CookieContainer cookie,string postData)
		{
			string html=null;
			try
			{
				//CookieContainer cookie = new CookieContainer();
				Encoding encoding = Encoding.GetEncoding("utf-8");
				string s = null;
				string requestUriString = null;

				requestUriString = url;
				s =postData;
				
				byte[] bytes = encoding.GetBytes(s);
				HttpWebRequest request = (HttpWebRequest) WebRequest.Create(requestUriString);
				request.Method = "POST";
				request.ContentType = "application/x-www-form-urlencoded";
				request.ContentLength = bytes.Length;
				Stream requestStream = request.GetRequestStream();
				requestStream.Write(bytes, 0, bytes.Length);
				requestStream.Close();
				string input = null;
				WebResponse response = request.GetResponse();
				Stream stream = response.GetResponseStream();
				StreamReader reader = new StreamReader(stream, encoding);
				char[] buffer = new char[0x100];
				for (int i = reader.Read(buffer, 0, 0x100); i > 0; i = reader.Read(buffer, 0, 0x100))
				{
					input = input + new string(buffer, 0, i);
				}
				reader.Close();
				stream.Close();
				response.Close();
				//string time = Regex.Match(input, @"\b\d+\.\d+\.\d+\b", RegexOptions.IgnoreCase).Value;
				html=input;
			}
			catch
			{
				html="";
			}
			return html;
        }
        #endregion 



    }


}
    